package com.zzy.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzy.commonutils.R;
import com.zzy.edu.entity.EduCourse;
import com.zzy.edu.entity.EduCourseDescription;
import com.zzy.edu.entity.vo.course.CourseInfoVo;
import com.zzy.edu.entity.vo.front.CourseFrontVo;
import com.zzy.edu.entity.vo.front.CourseWebVo;
import com.zzy.edu.entity.vo.publish.CoursePublishVo;
import com.zzy.edu.mapper.EduCourseMapper;
import com.zzy.edu.service.EduChapterService;
import com.zzy.edu.service.EduCourseDescriptionService;
import com.zzy.edu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.edu.service.EduVideoService;
import com.zzy.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author KAZU
 * @since 2023-01-13
 */
@SuppressWarnings("all")
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
	
	@Autowired
	private EduCourseMapper courseMapper;
	
	@Autowired
	private EduCourseDescriptionService courseDescriptionService;
	
	@Autowired
	private EduVideoService videoService;
	
	@Autowired
	private EduChapterService chapterService;
	
	/**
	 * 添加个人信息基本方法
	 * */
	@Override
	public String saveCourseInfo(CourseInfoVo courseInfoVo) {
		// 向课程表添加课程基本信息
		EduCourse eduCourse = new EduCourse();
		BeanUtils.copyProperties(courseInfoVo, eduCourse);
		int insert = courseMapper.insert(eduCourse);
		if(insert <= 0) {
			throw new GuliException(20001, "添加课程信息失败");
		}
		// 获取添加之后的课程ID
		String id = eduCourse.getId();
		// 向课程简介表添加课程简介
		EduCourseDescription courseDescription = new EduCourseDescription();
		courseDescription.setDescription(courseInfoVo.getDescription());
		courseDescription.setId(id);
		courseDescriptionService.save(courseDescription);
		return id;
	}
	
	/**
	 * 根据课程id查询课程基本信息
	 * */
	@Override
	public CourseInfoVo getCourseInfo(String courseId) {
		// 查询课程表
		EduCourse eduCourse = courseMapper.selectById(courseId);
		//	查询描述表
		EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
		// 封装
		CourseInfoVo courseInfoVo = new CourseInfoVo();
		BeanUtils.copyProperties(eduCourse, courseInfoVo);
		courseInfoVo.setDescription(courseDescription.getDescription());
		return courseInfoVo;
	}
	
	/**
	 * 修改课程信息
	 * */
	@Override
	public void updateCourseInfo(CourseInfoVo courseInfoVo) {
		// 修改课程表
		EduCourse eduCourse = new EduCourse();
		BeanUtils.copyProperties(courseInfoVo, eduCourse);
		int updateCourse = courseMapper.updateById(eduCourse);
		if (updateCourse == 0) {
			throw new GuliException(20001, "修改课程信息失败");
		}
		// 修改描述表
		EduCourseDescription courseDescription = new EduCourseDescription();
		courseDescription.setId(courseInfoVo.getId());
		courseDescription.setDescription(courseInfoVo.getDescription());
		boolean updateDescription = courseDescriptionService.updateById(courseDescription);
		if (!updateDescription) {
			throw new GuliException(20001, "修改课程信息失败");
		}
	}
	
	/**
	 * 根据课程id查询课程确认信息
	 * */
	@Override
	public CoursePublishVo publishCourseInfo(String courseId) {
		// 调用mapper
		CoursePublishVo publishCourseInfo = courseMapper.getPublishCourseInfo(courseId);
		
		return publishCourseInfo;
	}
	
	/**
	 * 根据课程id逻辑删除课程
	 * */
	@Override
	public void removeCourse(String courseId) {
		// 根据课程id删小节&视频
		videoService.removeVideoByCourseId(courseId);
		// 根据课程id删除章节
		chapterService.removeChapterByCourseId(courseId);
		// 根据课程id删除描述
		courseDescriptionService.removeDescriptionByCourseId(courseId);
		// 根据课程id删除课程本身
		int result = courseMapper.deleteById(courseId);
		if (result == 0) {
			throw new GuliException(20001, "删除失败");
		}
		//
		
		
	}
	
	/**
	 * 条件查询带分页查询课程
	 * */
	@Override
	public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {
		QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
			wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
		}
		if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
			wrapper.eq("subject_id", courseFrontVo.getSubjectId());
		}
		if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
			wrapper.orderByDesc("buy_count");
		}
		if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
			wrapper.orderByDesc("gmt_create");
		}
		if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
			wrapper.orderByDesc("price");
		}
		courseMapper.selectPage(coursePage, wrapper);
		List<EduCourse> records = coursePage.getRecords();
		long current = coursePage.getCurrent();
		long pages = coursePage.getPages();
		long size = coursePage.getSize();
		long total = coursePage.getTotal();
		boolean hasNext = coursePage.hasNext();
		boolean hasPrevious = coursePage.hasPrevious();
		
		HashMap<String, Object> map = new HashMap<>();
		map.put("items", records);
		map.put("current", current);
		map.put("pages", pages);
		map.put("size", size);
		map.put("total", total);
		map.put("hasNext", hasNext);
		map.put("hasPrevious", hasPrevious);
		
		return map;
	}
	
	/**
	 * 课程详情方法
	 * */
	@Override
	public CourseWebVo getBaseCourseInfo(String id) {
		return courseMapper.getBaseCourseInfo(id);
	}
	
	
}





